<?php

function count_works($bdd) {
    $req = $bdd->query('SELECT id_work FROM works');
    $count = $req->rowCount();
    return $count;
}
 function  select_id_user($bdd,$user_name)
 {
      $results = $bdd->query(" SELECT id_user FROM users WHERE user_name ='$user_name'"
);

    return $results;
 }

function model_works($bdd) {

    $results = $bdd->query('
       SELECT  u.user_name , u.user_surname , u.id_user,w.id_work,w.id_region, w.user_id author_user , w.work_name,w.work_description,w.img_type
       FROM users u
       INNER JOIN works w
       ON  u.id_user=w.user_id
       ORDER BY w.id_work
');

    return $results;
}

function model_update_works($bdd, $work_description, $work_id, $type_img, $work_name, $region, $user_name) { // premier enregistrement
    if ($type_img != 3) {

        $bdd->query("UPDATE works
            SET  work_description  = '$work_description',
	        img_type = $type_img,
                  user_id = '$user_name',
                      work_name ='$work_name',
                      id_region='$region'
           WHERE id_work = '$work_id' ");
    } else {
        $bdd->query("UPDATE works
            SET  work_description = '$work_description' ,
                user_name =   '$user_name',
                      work_name ='$work_name',
                      id_region='$region'
           WHERE id_work = '$work_id' ");
    }
}

function model_update_works2($bdd, $work_description, $work_id, $type_img, $work_name, $region, $user_name) { // premier enregistrement
    echo $type_img;
    if ($type_img != 3) {

        $bdd->query("UPDATE works
            SET  work_description  = '$work_description',
	        img_type2 = $type_img,
                  user_id = '$user_name',
                      work_name ='$work_name',
                      id_region='$region'
           WHERE id_work = '$work_id' ");
    } else {
        $bdd->query("UPDATE works
            SET  work_description = '$work_description' ,
                user_name =   '$user_name',
                      work_name ='$work_name',
                      id_region='$region'
           WHERE id_work = '$work_id' ");
    }
}

function model_update_Allworks($bdd, $work_description, $i, $type_img, $work_name, $region, $user_name) {

    if ($type_img != 3) {
        $bdd->query("UPDATE works
            SET  work_description  = '$work_description',
	        img_type = $type_img,
                  user_id = '$user_name',
                      work_name ='$work_name',
                       id_region='$region'
           WHERE id_work = '$i' ");
    } else {
        $bdd->query("UPDATE works
            SET  work_description  = '$work_description',
	        img_type = $type_img,
                  user_id = '$user_name',
                      work_name ='$work_name',
                 id_region='$region'

           WHERE id_work = '$i' ");
    }
}

function model_update_Allworks2($bdd, $work_description, $i, $type_img, $work_name, $region, $user_name) {

    if ($type_img != 3) {
        $bdd->query("UPDATE works
            SET  work_description  = '$work_description',
	        img_type2 = $type_img,
                  user_id = '$user_name',
                      work_name ='$work_name',
                       id_region='$region'
           WHERE id_work = '$i' ");
    } else {
        $bdd->query("UPDATE works
            SET  work_description  = '$work_description',
	        img_type = $type_img,
                  user_id = '$user_name',
                      work_name ='$work_name',
                 id_region='$region'

           WHERE id_work = '$i' ");
    }
}

function delete_work($bdd, $work_id) {
    $bdd->query("DELETE 
            FROM works
	    WHERE id_work = '$work_id'");
}

function select_user($bdd) {
    $results = $bdd->query("SELECT user_name
            FROM users
	  ");

    return $results;
}

?>
